﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FancyBattles.GameEntities;

namespace FancyBattles.GameScreen.StateMachine
{
    class DefineMoveState : State
    {

        private Vector2 shotDirection = Vector2.Zero;
        private float powerGauge = 1;
        public static Texture2D arrowDirectionBlue;
        public static Texture2D arrowDirectionRed;
        private BouncingBitch selectedBitch;
        private float angleRotation;

        public DefineMoveState(GameplayScreen parentScreen, PlayerIndex player)
            : base(parentScreen, player)
        {
            selectedBitch = (from bitch in BouncingBitch.BitchesList where bitch.IsSelected == true && bitch.Player.Equals(player) select bitch).First();
        }

        public override void Update(GameTime gameTime)
        {
            angleRotation = (float)(Math.Atan2(shotDirection.X, -shotDirection.Y)) - MathHelper.ToRadians(90);
            if (!double.IsNaN(InputHandler.Direction(player).X))
            {
                shotDirection = InputHandler.Direction(player);
            }
            if (InputHandler.isConfirm(player))
            {
                selectedBitch = (from bitch in BouncingBitch.BitchesList where bitch.IsSelected == true && bitch.Player.Equals(player) select bitch).First();
                selectedBitch.Speed = shotDirection;
                //selectedBitch.IsConfirmed = true;
                selectedBitch.directionArrowAngle = angleRotation;
                ParentScreen.ScreenStates[player] = new DefinePowerState(ParentScreen, player);
            }
            if (InputHandler.isCancel(player))
            {
                BouncingBitch selectedBitch = (from bitch in BouncingBitch.BitchesList where bitch.IsSelected == true && bitch.Player.Equals(player) select bitch).First();
                if (selectedBitch is Archer)
                {
                    ParentScreen.ScreenStates[player] = new DefineShotState(ParentScreen, player);
                }
                else
                {
                    ParentScreen.ScreenStates[player] = new SelectState(ParentScreen, player);
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if(player.Equals(PlayerIndex.One))
            {
                ParentScreen.ScreenManager.SpriteBatch.Draw(arrowDirectionBlue, new Vector2(selectedBitch.Position.X + 15, selectedBitch.Position.Y + 15), null, Color.White, angleRotation, new Vector2(0, 58), 1, SpriteEffects.None, 0);
            }
            else
            {
                ParentScreen.ScreenManager.SpriteBatch.Draw(arrowDirectionRed, new Vector2(selectedBitch.Position.X + 15, selectedBitch.Position.Y + 15), null, Color.White, angleRotation, new Vector2(0, 58), 1, SpriteEffects.None, 0);
            }
        }

    }
}
